
(function(window,document){
    const rootElement = document.compatMode=="CSS1Compat"?document.documentElement:document.body;
    const appHeader = document.getElementById("app-header");
    const waitingElement = document.getElementById("waiting");
    const appElement = document.getElementById("app");
    const loginElement = document.getElementById("login-form");
    const loginBtn = document.getElementById("login-btn");
    
    const workarea = document.getElementById("workarea");
    const nav = document.getElementById("nav");
    const chats = document.getElementById("chats");
    const chatlist = document.getElementById("chat-list");
    const search = document.getElementById("search");

    

    let chatApp=window.chatApp={};
    let isWaiting=false;
    chatApp.waiting=function(txt){
        let centerWaiting = function(){
            waitingElement.style.top = (rootElement.clientHeight - waitingElement.clientHeight)/2;
            waitingElement.style.left = (rootElement.clientWidth - waitingElement.clientWidth)/2;
        };
        if(txt!==false){
            waitingElement.style.display="block";
            chatApp.showMain(false);
            chatApp.login(false);
            loginElement.style.display = "none";    
            if(txt===true || txt===undefined || txt===null) txt="";
            waitingElement.lastElementChild.innerHTML = txt;
            centerWaiting();
            if(!isWaiting)window.addEventListener("resize",centerWaiting);
            isWaiting = true;
        }else {
            waitingElement.style.display="none";
            if(isWaiting) window.removeEventListener("resize",centerWaiting);
            isWaiting = false;
        }
        return this;
    }

    let isLoginShow =false;
    chatApp.login=function(onSubmit){
        let centerLogin = function(){
            loginElement.style.top = (rootElement.clientHeight - loginElement.clientHeight)/2;
            loginElement.style.left = (rootElement.clientWidth - loginElement.clientWidth)/2;
        };

        if(onSubmit===false){
            loginElement.style.display="none";
            if(isLoginShow) window.removeEventListener("resize",centerLogin);
            isLoginShow = false;
        }else{
            loginElement.style.display="block";
            chatApp.showMain(false);chatApp.waiting(false);
            
            centerLogin();
            if(!isLoginShow) window.addEventListener("resize",centerLogin);
            isLoginShow = true;
            if(typeof onSubmit==="function") loginBtn.onclick = onSubmit;
        }
    }
    let isMainShow;
    chatApp.showMain = function(onSend){
        let adjustSizes = function(){
            const currChat = document.getElementById("current-chat");
            const currChatCaption = document.getElementById("current-chat-caption");
            const messages = document.getElementById("messages");
            const opElement = document.getElementById("op");
            let h = rootElement.clientHeight-2;
            chats.style.height = currChat.style.height = nav.style.height = h + "px";
            chatlist.style.height = h-search.clientHeight- appHeader.clientHeight + "px";
            messages.style.height = h- currChatCaption.clientHeight - opElement.clientHeight - appHeader.clientHeight + "px";
        };
        if(onSend!==false){
            appElement.style.display = "block";
            chatApp.waiting(false);chatApp.login(false);
            if(!isMainShow) window.addEventListener("resize",adjustSizes);
            adjustSizes();
            if(typeof onSend==="function"){
                document.getElementById("send-btn").onclick = onSend;
            }
            isMainShow = true;
        }else{
            appElement.style.display ="none";
            if(isMainShow) window.removeListener("resize",adjustSizes);
            isMainShow = false;
        }
    }
    chatApp.attechWindowEvents=function(opts){

    }

    chatApp.waiting("启动中...");
})(window,document);


